/**
 * @author yzhou2
 */
if(!QSAPI){
	var QSAPI={};
}
if(!QSAPI.Util){
	QSAPI.Util={};
}

QSAPI.Util.DateTime={
	formatMDYDate:function(dt){//dt Date
        return (dt==null)?" " : DTFormat.Mo[dt.getMonth()]+" "+(dt.getDate()<10?"0"+dt.getDate():dt.getDate())+", "+dt.getFullYear();
    },
    transMDYDate:function(d){//yyyy-mm-dd
       if(d==null){
            return '';
       }else{
           var arr=d.split('-'),
              y=arr[0],
              m=parseInt(arr[1],10)-1,
              day=arr[2];
           return DTFormat.Mo[m]+" "+day+","+y;
       }
       
    },
    formatDate:function(date){//Date
        if (date == null) {
            return null;
        }
        return date.getFullYear()+"-"+this.formateNum(date.getMonth()+1)+"-"+this.formateNum(date.getDate());
    },
    formatDateTime:function(date){//Date
        if (date == null) {
            return null;
        }
        return date.getFullYear()+"-"+this.formateNum(date.getMonth()+1)+"-"+this.formateNum(date.getDate())+"T"+this.formateNum(date.getHours()) + ":" + this.formateNum(date.getMinutes())+":"+this.formateNum(date.getSeconds());
    },
	compareWithToday:function(date){//Date
        var today=this.formatDate(new Date());
		d=this.formatDate(date);
        var arr=today.split('-');
        var tStr=arr.join('');
        var tInt=parseInt(tStr,10);
        var arr1=d.split('-');
        var dStr=arr1.join('');
        var dInt=parseInt(dStr,10);
        return dInt-tInt;
    },
    formateNum:function(d){
        return d < 10 ? "0" + d : d.toString(); 
    },
    convertDate:function(d){//convert yyyy-mm-dd to date format
        var arr=d.split('-');
        return new Date(parseInt(arr[0],10),parseInt(arr[1],10)-1,parseInt(arr[2],10));
    },
	convertToDate:function(d){//convert yyyy/mm/dd'T'HH:mm:ss to date format
        d=d.split('T');
		var dt=d[0].split('-'),tm=d[1].split(':');
		return new Date(parseInt(dt[0],10),parseInt(dt[1],10)-1,parseInt(dt[2],10),parseInt(tm[0],10),parseInt(tm[1],10),parseInt(tm[2],10));
    },
    setTime:function(date,type){
        var h=0,m=0,s=0;
        if(type=='now'){
            var now=new Date();
            h=now.getHours();
            m=now.getMinutes();
            s=now.getSeconds();
        }else if(type=='max'){
            h=23;
            m=59;
            s=59;   
        }
        date.setHours(h);
        date.setMinutes(m);
        date.setSeconds(s);
        return date;
    },
    toUSDate:function(d){
         if (d == ""&&t=="") {
            return "";
        }
        var date=d.split("T")[0];
        var time=d.split("T")[1].substring(0,5);
        var ds = date.split("-");
        
        var month = parseInt(ds[1], 10),
            day = parseInt(ds[2], 10);  
        return month+"/"+day+" "+time;
    },
    toDateTime:function(d){
        if (d == ""&&t=="") {
            return "";
        }
        var date=d.split("T")[0];
        var time=d.split("T")[1].substring(0,5);
        var ds = date.split("-");
        return parseInt(ds[1], 10)+"/"+parseInt(ds[2], 10)+"/"+parseInt(ds[0], 10)+", "+time;
    }
};

if(!QSAPI.Widget){
	QSAPI.Widget={};
}

QSAPI.Widget.NewsDataManager=function(){
	var qsPath=QSAPI.getQSPath();
	this.dataURL={
        newsOverview:qsPath+"getNewsOverview.jsp",
        newsTitle:qsPath+"getNewsTitle.jsp",
        newsContent:qsPath+"getNewsContent.jsp",
        newsSource:qsPath+"getNewsSource.jsp",
        template:qsPath+"template.jsp"
    };
	
	this.sourceGroupList=[{txt:'All',val:'msview',vid:-1}];
	this.sourceGroup={
        '-1':{      
            'ntSource':['DJNMNDJEN','DJNMNDJBN','DJNMNBW','DJNMNPR','DJNMNCW','DJNMNCNCN','DJNMNPMZN','DJNMNMKTW','4028']
        }
	},
	this.defaultOpenedList=[-1];
	

};

QSAPI.Widget.NewsDataManager.prototype={
	getSourceGroupList:function(type,callback){
		callback(this.sourceGroupList);
    },
	getOpenedList:function(openedList){
		if(typeof openedList=='undefined'||openedList.length==0){
			return this.defaultOpenedList;
		}else{
			var realOpenedlist=[];
			for(var i=0,l=openedList.length;i<l;i++){
				if(this.sourceGroup[openedList[i]]){
					realOpenedlist.push(openedList[i]);
				}
			}
			return realOpenedlist;
		}
	},
	getSourceGroupByID:function(id,callback){
		if(this.sourceGroup[id]){
			callback(this.sourceGroup[id])
		}else{//remote to get
			
		}
	},
	getSourceGroupNameByID:function(id){
		var sourceGroup,name="";
		for(var i=0,l=this.sourceGroupList.length;i<l;i++){
			sourceGroup=this.sourceGroupList[i];
			if(id==sourceGroup.vid){
				name=sourceGroup.txt;
				break;
			}
		}
		return name;
	},
	getSourceGroupID:function(id,openedList){
		if(typeof id=='undefined'){
			return openedList[0];
		}else{
			if(!this.sourceGroup[id]){
				return openedList[0];
			}else{
				return id;
			}
		}
	},
	getNewsTitleList:function(data,callback){
        $.getJSON(this.dataURL.newsTitle+"?cb=?",data,function(o){
			var list=o;
			if(typeof o=='string'){
				list= eval("(" + o + ")");
			}
            if(typeof callback == "function"){
                callback(list);
            }
        });
    },
	getNewsContent:function(data,callback){
		$.getJSON(this.dataURL.newsContent+"?cb=?",data,function(o){
			if(typeof o=='string'){
				o= eval("(" + o + ")");
			}
            if(typeof callback == "function"){
                callback(o);
            }
        });
	}
}